package com.gs.synedocket.presenter;

import com.google.gson.reflect.TypeToken;
import com.gs.synedocket.apiresult.ApiResultData;
import com.gs.synedocket.db.dto.ScanPaperDto;
import com.gs.synedocket.model.DocketModel;
import com.gs.synedocket.model.UtilModel;
import com.gs.synedocket.model.listener.OnResultListener;
import com.gs.synedocket.view.IScanPaperView;

import java.lang.reflect.Type;

public class ScanPaperPresenter extends BasePresenter<IScanPaperView> {
    private UtilModel utilsModel = new UtilModel();
    private DocketModel docketModel = new DocketModel();

    public void upload(String path) {
        if (getView() != null) {
            getView().showLoading();
            Type typeofT=new TypeToken<ApiResultData<String>>(){}.getType();
            utilsModel.uploadOffline(path, new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onUploadFileSuccess((ApiResultData<String>)aipResult);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onUploadFileFails(e);
                    }
                }
            });
        }
    }

    public void scanPaper(ScanPaperDto bean) {
        if (getView() != null) {
            docketModel.scanPaper(bean, new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onScanPaperSuccess((ApiResultData<String>)aipResult);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().hideLoading();
                        getView().onScanPaperFails(e);
                    }
                }
            });
        }
    }
}
